﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.MissionViews.Singleplayer.DeploymentMissionView
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using TaleWorlds.Core;
using TaleWorlds.MountAndBlade.View.MissionViews.Order;

#nullable disable
namespace TaleWorlds.MountAndBlade.View.MissionViews.Singleplayer
{
  public class DeploymentMissionView : MissionView
  {
    private OrderTroopPlacer _orderTroopPlacer;
    private MissionDeploymentBoundaryMarker _deploymentBoundaryMarkerHandler;
    private MissionEntitySelectionUIHandler _entitySelectionHandler;
    public OnPlayerDeploymentFinishDelegate OnDeploymentFinish;

    public override void AfterStart()
    {
      this._orderTroopPlacer = this.Mission.GetMissionBehavior<OrderTroopPlacer>();
      this._entitySelectionHandler = this.Mission.GetMissionBehavior<MissionEntitySelectionUIHandler>();
      this._deploymentBoundaryMarkerHandler = this.Mission.GetMissionBehavior<MissionDeploymentBoundaryMarker>();
    }

    public override void OnInitialDeploymentPlanMadeForSide(BattleSideEnum side, bool isFirstPlan)
    {
      if (side != this.Mission.PlayerTeam.Side || !this.Mission.DeploymentPlan.HasDeploymentBoundaries(this.Mission.PlayerTeam.Side))
        return;
      this._orderTroopPlacer?.RestrictOrdersToDeploymentBoundaries(true);
    }

    public override void OnDeploymentFinished()
    {
      this.OnDeploymentFinish();
      if (this._entitySelectionHandler != null)
        this.Mission.RemoveMissionBehavior((MissionBehavior) this._entitySelectionHandler);
      if (this._deploymentBoundaryMarkerHandler != null)
      {
        if (this.Mission.DeploymentPlan.HasDeploymentBoundaries(this.Mission.PlayerTeam.Side))
          this._orderTroopPlacer?.RestrictOrdersToDeploymentBoundaries(false);
        this.Mission.RemoveMissionBehavior((MissionBehavior) this._deploymentBoundaryMarkerHandler);
      }
      if (this.Mission.HasMissionBehavior<MissionBoundaryWallView>())
        return;
      this.MissionScreen.AddMissionView((MissionView) new MissionBoundaryWallView());
    }
  }
}
